/*
 * @(#)MaintDmsGroupRoleFolderAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsObjectHomeFolder;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.DmsObjectHomeFolderDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.UserErrorConstant;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserMember;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  MaintDmsGroupRoleFolderAction.java
  This class is for Dms Group Role Folder Setup Function.
    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   24/11/2004
    @version         $Revision: 1.8 $
 */

public class ListDmsGroupRoleFolderAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.8 $";

  protected String parentID = "";
  protected String rootID = "";
  protected final static String MESSAGE_RECORD_INSERT = "common.message.record_inserted";
  
  public ListDmsGroupRoleFolderAction() {
    super();
    this.setListName("memberList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return ("com.dcivision.dms.dao.DmsObjectHomeFolderDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return (SystemFunctionConstant.DMS_ADMIN);
  }
  
  /**
   *  Method execute() - generaic function of result list handling               
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    //  Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    ListDmsGroupRoleFolderForm actionForm=(ListDmsGroupRoleFolderForm)form;
    parentID = actionForm.getParentID();
    rootID = actionForm.getRootID();
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DmsDocument documentRoot=new DmsDocument();
    if (Utility.isEmpty(parentID) || "null".equals(parentID) ||
        Utility.isEmpty(rootID) || "null".equals(rootID)) {
      DmsRoot personalRoot = rootRetrievalManager.getRootObjectByOwnerID(sessionContainer.getUserRecordID());
      documentRoot = docRetrievalManager.getRootFolderByRootID(personalRoot.getID());
      parentID = TextUtility.formatIntegerObj(documentRoot.getParentID());
      rootID = TextUtility.formatIntegerObj(personalRoot.getID());
      actionForm.setRootID(rootID);
      actionForm.setParentID(parentID);
    }
    documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(actionForm.getRootID()));
    request.setAttribute("rootFolder", documentRoot);
    String path="";
    request.setAttribute("location", path);
    AbstractSearchForm searchForm=(AbstractSearchForm)form;
    //combine group with folder
    if(UserMember.OPMODE_ADD_GROUP.equals(actionForm.getOpMode())){
      try{
        this.addUserGroup(mapping,form,request,response);
        this.commit(request);
        this.getListData(mapping,searchForm,request,response);
        String error=(String)request.getAttribute("error");
        if(!Utility.isEmpty(error)){
          addError(request,error);  
        }else{
          addMessage(request,MESSAGE_RECORD_INSERT); 
        }
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      actionForm.setOpMode("");
      actionForm.setUserGroupID("");
      actionForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }
    //combine role with folder
    else if(UserMember.OPMODE_ADD_ROLE.equals(actionForm.getOpMode())){
      try{
        this.addUserRole(mapping,form,request,response);
        this.commit(request);
        this.getListData(mapping,searchForm,request,response);
        String error=(String)request.getAttribute("error");
        if(!Utility.isEmpty(error)){
          addError(request,error);  
        }else{
          addMessage(request,MESSAGE_RECORD_INSERT); 
        }
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      actionForm.setOpMode("");
      actionForm.setUserRoleID("");
      actionForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else{
      //this.getListData(mapping,form,request,response);
      return super.execute(mapping,form,request,response);
    }
  }
   
  /**
   * get all list date
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsGroupRoleFolderForm actionForm=(ListDmsGroupRoleFolderForm)form;
    DmsObjectHomeFolderDAObject dmsGroupRoleFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    List memberList=new ArrayList();
    memberList=dmsGroupRoleFolderDAO.getGroupRoleList(form);   
    String memberName="";
    String memberDes="";
    for(int i=0;i<memberList.size();i++){
      DmsObjectHomeFolder dmsObjectHomeFolder=(DmsObjectHomeFolder)memberList.get(i);
      String location=docRetrievalManager.getLocationPath(dmsObjectHomeFolder.getDocumentID());
      if(DmsObjectHomeFolder.MEMBER_TYPE_GROUP.equals(dmsObjectHomeFolder.getMemberType())){
        UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
        memberName=userGroup.getGroupName();
        memberDes = userGroup.getGroupDesc();
      }else if(DmsObjectHomeFolder.MEMBER_TYPE_ROLE.equals(dmsObjectHomeFolder.getMemberType())){
        UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsObjectHomeFolder.getMemberID());
        memberName=userRole.getRoleName();
        memberDes = userRole.getRoleDesc();
      }
      dmsObjectHomeFolder.setLocation(location);
      dmsObjectHomeFolder.setMemberName(memberName);
      dmsObjectHomeFolder.setMemberDesc(memberDes);
    }
    request.setAttribute(this.getListName(),memberList);
  }
  
  /**
   * this function is to add group.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsGroupRoleFolderForm actionForm=(ListDmsGroupRoleFolderForm)form;
    Integer userGroupID=TextUtility.parseIntegerObj(actionForm.getUserGroupID());
    Integer documentID=TextUtility.parseIntegerObj(actionForm.getDestinationID());
    DmsObjectHomeFolderDAObject dmsGroupRoleFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
    
    DmsObjectHomeFolder dmsGroupRoleFolder=new DmsObjectHomeFolder();
    dmsGroupRoleFolder.setMemberID(userGroupID);
    dmsGroupRoleFolder.setDocumentID(documentID);
    dmsGroupRoleFolder.setMemberType(DmsObjectHomeFolder.MEMBER_TYPE_GROUP);
    dmsGroupRoleFolder.setItemStatus(DmsVersion.AVAILABLE_STATUS);
    //check has double record or not.
    List list=dmsGroupRoleFolderDAO.getByMemberIDDocumentID(userGroupID,DmsObjectHomeFolder.MEMBER_TYPE_GROUP);
    if(list.size()>0){
      request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
    }else{
      dmsGroupRoleFolderDAO.insertObject(dmsGroupRoleFolder);
    }
  }
  
  /**
   * this function is to add role.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsGroupRoleFolderForm actionForm=(ListDmsGroupRoleFolderForm)form;
    Integer userRoleID=TextUtility.parseIntegerObj(actionForm.getUserRoleID());
    Integer documentID=TextUtility.parseIntegerObj(actionForm.getDestinationID());
    DmsObjectHomeFolderDAObject dmsGroupRoleFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
    DmsObjectHomeFolder dmsGroupRoleFolder=new DmsObjectHomeFolder();
    dmsGroupRoleFolder.setMemberID(userRoleID);
    dmsGroupRoleFolder.setDocumentID(documentID);
    dmsGroupRoleFolder.setMemberType(DmsObjectHomeFolder.MEMBER_TYPE_ROLE);
    dmsGroupRoleFolder.setItemStatus(DmsVersion.AVAILABLE_STATUS);
    //check has double record or not.
    List list=dmsGroupRoleFolderDAO.getByMemberIDDocumentID(userRoleID,DmsObjectHomeFolder.MEMBER_TYPE_ROLE);
    if(list.size()>0){
      request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
    }else{
      dmsGroupRoleFolderDAO.insertObject(dmsGroupRoleFolder);
    }
    
  }
  
}
  